package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.CntrSize;
import com.hzhc.entity.CntrType;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.mapper.CntrSizeMapper;
import com.hzhc.mapper.CntrTypeMapper;
import com.hzhc.service.CntrSizeService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.CntrSizeVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 箱尺寸代码 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@Service
@RequiredArgsConstructor
public class CntrSizeServiceImpl extends ServiceImpl<CntrSizeMapper, CntrSize> implements CntrSizeService {
    private final CntrTypeMapper cntrTypeMapper;

    /**
     * 分页查询
     * @param vo     查询条件
     * @param page   分页对象
     * @return List<CntrSize>  箱尺寸集合数据
     */
    @Override
    public List<CntrSizeVo> getCntrSizeDataByPage(CntrSizeVo vo, Page<CntrSizeVo> page) {
        IPage<CntrSizeVo> iPage = baseMapper.selectCntrSizePage(page,vo);
        return iPage.getRecords();
    }

    /**
     * 新增箱尺寸代码
     * @param cntrSize   箱尺寸代码
     * @return  ResultUtil<T>
     */
    @Override
    public ResultUtil<T> addCntrSize(CntrSize cntrSize) {
        //重复校验
        ResultUtil<T> result = checkSame(cntrSize);
        if (result!=null){
            return result;
        }
        boolean b = save(cntrSize);
        if(b){
            return ResultUtil.build(ResultUtilCodeEnum.SUCCESS);
        }else{
            return ResultUtil.build(ResultUtilCodeEnum.FAIL);
        }
    }

    /**
     * 修改箱尺寸代码
     * @param cntrSize  箱尺寸代码
     * @return ResultUtil<T>
     */
    @Override
    public ResultUtil<T> updateCntrSize(CntrSize cntrSize) {
        //重复校验
        ResultUtil<T>  result = checkSame(cntrSize);
        if (result!=null){
            return result;
        }
        boolean b = updateById(cntrSize);
        if(b){
            return ResultUtil.build(ResultUtilCodeEnum.SUCCESS);
        }else{
            return ResultUtil.build(ResultUtilCodeEnum.FAIL);
        }
    }

    /**
     * 删除箱尺寸代码
     * @param id 箱尺寸代码id
     * @return   ResultUtil<T>
     */
    @Override
    public ResultUtil<T> deleteCntrSize(Long id) {
        CntrSize cntrSize = getById(id);
        cntrSize.setDisable(1)  ;
        boolean b = baseMapper.updateById(cntrSize)>0;
        if(b){
            return ResultUtil.build(ResultUtilCodeEnum.SUCCESS);
        }else{
            return ResultUtil.build(ResultUtilCodeEnum.FAIL);
        }
    }

    @Override
    public ResultUtil<T> checkCntrSize(CntrSize cntrSize) {
       return checkSame(cntrSize);
    }

    @Override
    public List<CntrSizeVo> getCntrSizeList(CntrSizeVo vo) {
        return baseMapper.selectCntrSizePage(vo);
    }

    @Override
    public List<CntrSizeVo> getCntrSizeTypeList(CntrSizeVo vo) {
        List<CntrSizeVo> cntrSizeList = baseMapper.getCntrSizeList(vo);
        List<CntrType> cntrTypeList = cntrTypeMapper.selectList(Wrappers.<CntrType>lambdaQuery().eq(CntrType::getDisable,0));
        Map<Long, CntrType> cntrTypeMap = cntrTypeList.stream().collect(Collectors.toMap(CntrType::getId, cntrType -> cntrType));
        List<CntrSizeVo> returnList = new ArrayList<>();
        for (CntrSizeVo cntrSizeVo : cntrSizeList) {
            CntrSizeVo sizeVo=new CntrSizeVo();
            sizeVo.setCntrSize(cntrSizeVo.getCntrSize()+cntrTypeMap.get(cntrSizeVo.getCntrTypeId()).getTypeCode());
            sizeVo.setCntrSizeType(cntrSizeVo.getCntrSize()+cntrTypeMap.get(cntrSizeVo.getCntrTypeId()).getTypeCode()+"-"+cntrSizeVo.getSelfUserType());
            returnList.add(sizeVo);
        }
        return returnList;
    }


    /**
     * 重复校验
     * @param cntrSize   箱尺寸代码
     * @return ResultUtil<T>
     */
    public ResultUtil<T> checkSame(CntrSize cntrSize){
        //重复校验等具体需求修改
        boolean b = !list(Wrappers.<CntrSize>lambdaQuery()
                .eq(CntrSize::getCntrSize, cntrSize.getCntrSize())
                .eq(CntrSize::getCntrTypeId,cntrSize.getCntrTypeId())
                .ne(cntrSize.getId() != null, CntrSize::getId, cntrSize.getId())
        ).isEmpty();
        if (b) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_SIZE_CODE_CNTR_TYPE_EXIST);
        }
        b = !list(Wrappers.<CntrSize>lambdaQuery()
                .eq(CntrSize::getCntrName, cntrSize.getCntrName())
                .ne(cntrSize.getId() != null, CntrSize::getId, cntrSize.getId())
        ).isEmpty();
        if (b) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_SIZE_NAME_EXIST);
        }
        return null;
    }
}
